package com.cc.servlet;

import com.alibaba.fastjson.JSON;
import com.cc.entity.AttendanceRecord;
import com.cc.entity.Page;
import com.cc.service.AttendanceService;
import com.cc.service.impl.AttendanceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/attendance")
public class AttendanceServlet extends HttpServlet {
    private AttendanceService attendanceService = new AttendanceServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "clockIn":
                clockIn(req,resp);
                break;
            case "getAttendanceRecords":
                getAttendanceRecords(req,resp);
                break;
            case "getAttendanceOverview":
                getAttendanceOverview(req,resp);
                break;
            default:
                break;
        }
    }

    private void getAttendanceOverview(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String employeeId = req.getParameter("employeeId");
        Map<String, Object> result = new HashMap<>();

        try {
            if (employeeId == null || employeeId.isEmpty()) {
                throw new IllegalArgumentException("员工ID不能为空");
            }

            Map<String, Object> overview = attendanceService.getAttendanceOverview(Integer.parseInt(employeeId));

            result.put("code", "200");
            result.put("msg", "获取考勤概览成功");
            result.put("data", overview);

        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "获取考勤概览失败: " + e.getMessage());
        }

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().print(JSON.toJSONString(result));
    }

    /**
     * 获取考勤记录
     * @param req
     * @param resp
     * @throws IOException
     */
    private void getAttendanceRecords(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String employeeId = req.getParameter("employeeId");
        Map<String, Object> result = new HashMap<>();

        try {
            // 参数校验
            if (employeeId == null || employeeId.isEmpty()) {
                throw new IllegalArgumentException("员工ID不能为空");
            }

            // 查询数据 - 这里简化了分页逻辑，只查询最近的10条记录
            Page<AttendanceRecord> page = new Page<>();
            page.setPageNum(1);
            page.setPageSize(10);

            Page<AttendanceRecord> records = attendanceService.getAttendanceRecords(Integer.parseInt(employeeId), page);

            // 构建返回结果
            result.put("code", "200");
            result.put("msg", "查询成功");
            result.put("data", records.getData());

        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "查询失败: " + e.getMessage());
        }

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().print(JSON.toJSONString(result));
    }

    private void clockIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String employeeId = req.getParameter("employeeId");
        // 直接使用服务层返回的结果
        Map<String, Object> result = attendanceService.isClockIn(employeeId);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().print(JSON.toJSONString(result));
    }
}
